package com.example.studymoudle.observer_design_pattern;

public class ObservableCreate<T> extends Observable<T>{
    ObservaleOnSubscribe<T> source;
    public ObservableCreate(ObservaleOnSubscribe<T> source) {
        this.source = source;
    }

    @Override
    public void subscribe(Observer observer) {
        super.subscribe(observer);
    }

    @Override
    protected void subscribeActual(Observer observer) {
        //如果订阅功能要实现，在这里就应该通知observer消息通知了
        //observer.onComplete();
        CreateEmitter<T> parent = new CreateEmitter<>(observer);
        observer.onSubscribe();
       // 把发射器和被观察者绑定的一起（订阅）
        source.subscribe(parent);

    }


    static  final class CreateEmitter<T> implements Emitter<T>{
        Observer<T> observer;

        public CreateEmitter(Observer<T> observer) {
            this.observer = observer;
        }

        @Override
        public void onNext(T t) {
            observer.onNext(t);
        }

        @Override
        public void onError(Throwable throwable) {
            observer.onError(throwable);
        }

        @Override
        public void onComplete() {
            observer.onComplete();
        }
    }
}
